package com.remote.common.model.fx;


import com.remote.common.model.MouseButton;
import lombok.Data;
import lombok.ToString;

/**
 * @author Cool-Coding
 * 2018/7/26
 */
@Data
@ToString
public class MasterMouseEvent {

    /**
     * 1.单击
     * 2.双击
     * 3.拖拽
     * 4.移动
     * 5.滚轮滚动
     * 6.按下
     * 7.释放
     */
    private byte status;

    /**
     * 鼠标按键
     */
    private MouseButton mouseButton;
    /**
     * 滚轮滚动的大小
     */
    private int mouseWheel;
    /**
     * X坐标
     */
    private double x;
    /**
     * Y坐标
     */
    private double y;


    public void buttonClicked(javafx.scene.input.InputEvent inputEvent, int x, int y) {
        this.status = 1;
        this.mouseButton = getMouseByFX(inputEvent);
        this.x = x;
        this.y = y;
    }


    public void buttonDoubleClick(javafx.scene.input.InputEvent inputEvent, int x, int y) {
        this.status = 2;
        this.mouseButton = getMouseByFX(inputEvent);
        this.x = x;
        this.y = y;
    }


    public void dragged(javafx.scene.input.InputEvent inputEvent, int x, int y) {
        this.status = 3;
        this.mouseButton = getMouseByFX(inputEvent);
        this.x = x;
        this.y = y;
    }


    public void mouseMoved(int x, int y) {
        this.status = 4;
        this.x = x;
        this.y = y;
    }


    public void mouseWheel(int mouseWheel) {
        this.status = 5;
        this.mouseWheel = mouseWheel;
    }

    public void mousePressed(javafx.scene.input.InputEvent inputEvent) {
        this.status = 6;
        this.mouseButton = getMouseByFX(inputEvent);
    }

    public void mouseReleased(javafx.scene.input.InputEvent inputEvent) {
        this.status = 7;
        this.mouseButton = getMouseByFX(inputEvent);
    }


    public boolean isClicked() {
        return this.status == 1;
    }

    public boolean isDoubleClicked() {
        return this.status == 2;
    }

    public boolean isDragged() {
        return this.status == 3;
    }

    public boolean isMouseMoved() {
        return this.status == 4;
    }

    public boolean isMouseWheel() {
        return this.status == 5;
    }

    public boolean isMousePressed() {
        return this.status == 6;
    }

    public boolean isMouseReleased() {
        return this.status == 7;
    }

    public int getJWTMousea() {
        if (mouseButton == null) {
            return 0;
        }
        switch (mouseButton) {
            case LEFT:
                return java.awt.event.InputEvent.BUTTON1_DOWN_MASK;
            case MIDDLE:
                return java.awt.event.InputEvent.BUTTON2_DOWN_MASK;
            case RIGHT:
                return java.awt.event.InputEvent.BUTTON3_DOWN_MASK;
            case PRESSED:
                return java.awt.event.MouseEvent.MOUSE_PRESSED;
            case DRAGGED:
                return java.awt.event.InputEvent.BUTTON1_DOWN_MASK;
            case RELEASED:
                return java.awt.event.MouseEvent.MOUSE_RELEASED;
            case WHEEL:
                return java.awt.event.MouseEvent.MOUSE_WHEEL;
            case CLICK:
                return java.awt.event.MouseEvent.MOUSE_CLICKED;
            default:
                throw new RuntimeException("wrong MouseButton value:" + mouseButton);
        }
    }

    protected MouseButton getMouseByFX(javafx.scene.input.InputEvent inputEvent) {
        if (inputEvent instanceof javafx.scene.input.MouseEvent
                && javafx.scene.input.MouseButton.PRIMARY == ((javafx.scene.input.MouseEvent) inputEvent).getButton()) {
            return com.remote.common.model.MouseButton.LEFT;
        } else if (inputEvent instanceof javafx.scene.input.MouseEvent
                && javafx.scene.input.MouseButton.MIDDLE == ((javafx.scene.input.MouseEvent) inputEvent).getButton()) {
            return com.remote.common.model.MouseButton.MIDDLE;
        } else if (inputEvent instanceof javafx.scene.input.MouseEvent
                && javafx.scene.input.MouseButton.SECONDARY == ((javafx.scene.input.MouseEvent) inputEvent).getButton()) {
            return com.remote.common.model.MouseButton.RIGHT;
        } else if (inputEvent instanceof javafx.scene.input.MouseDragEvent) {
            return com.remote.common.model.MouseButton.DRAGGED;
        } else if (inputEvent instanceof javafx.scene.input.ScrollEvent) {
            return com.remote.common.model.MouseButton.WHEEL;
        } else if (javafx.scene.input.MouseEvent.MOUSE_CLICKED == inputEvent.getEventType()) {
            return com.remote.common.model.MouseButton.CLICK;
        } else if (javafx.scene.input.MouseEvent.MOUSE_PRESSED == inputEvent.getEventType()) {
            return com.remote.common.model.MouseButton.PRESSED;
        } else if (javafx.scene.input.MouseEvent.MOUSE_RELEASED == inputEvent.getEventType()) {
            return com.remote.common.model.MouseButton.RELEASED;
        } else {
            throw new RuntimeException("wrong MouseEvent value:" + inputEvent);
        }

    }

}
